export function arrTomap(arr) {
  const map = {}
  arr.forEach(item => {
    map[item.id] = item
  })
  const list = []
  data.forEach(item => {
    let parent = map[item.pid]
    if(parent) {
      (parent.children || (parent.children = []).push(item))
    } else {
      list.push(item)
    }
  })
  return list
}

export function useApi(api) {
  const loading = ref(false)
  const result = ref(null)
  const error = ref(null)

  const fetchResource = params => {
    loading.value = true
    return api(params).then(res => {
      result.value = res
    }).catch(err => {
      error.value = err
    }).finally(() => {
      loading.value = false
    })
  }

  return {
    loading,
    error,
    result,
    fetchResource
  }
}